﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SHW_UP.BP;

namespace SHW_UP.UI
{
    public partial class frmMobile : Form
    {

        public frmMobile()
        {
            InitializeComponent();
        }

        private DataTable dt = new DataTable();
        private void frmMobile_Load(object sender, EventArgs e)
        {
            this.mobileGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.mobileGridView.Columns.Clear();
            this.mobileGridView.ReadOnly = true;
            dt.Columns.Add("电话号码");
            List<string> list = (List<string>)InfoUtils.parTable["mobile"];
            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr1 = dt.NewRow();
                dr1[0] = list[i];
                dt.Rows.Add(dr1);
            }
            this.mobileGridView.DataSource = dt;
        }

        private void mobileAdd_Click(object sender, EventArgs e)
        {
            string passwd=string.Empty;
            if(InfoUtils.parTable.Contains("passwd"))
            {
                passwd=(string)InfoUtils.parTable["passwd"];
            }else
            {
                passwd="000000";
            }
            string str = "ZJHM,"+passwd+","+this.textBox1.Text+",";
            if (InfoUtils.sendInfoTable.ContainsKey("ZJHM"))
            {
                InfoUtils.sendInfoTable.Remove("ZJHM");
            }
                InfoUtils.sendInfoTable.Add("ZJHM", str);
            DataRow dr1 = dt.NewRow();
            dr1[0] = this.textBox1.Text;
            dt.Rows.Add(dr1);
            //SerialPortSender.sendStrInfo(str,this.serialPort);
        }

        private void mobileDel_Click(object sender, EventArgs e)
        {
            string passwd = string.Empty;
            if (InfoUtils.parTable.Contains("passwd"))
            {
                passwd = (string)InfoUtils.parTable["passwd"];
            }
            else
            {
                passwd = "000000";
            }
            string str = "SCHM," + passwd + "," + this.textBox1.Text + ",";
            if (InfoUtils.sendInfoTable.ContainsKey("SCHM"))
            {
                InfoUtils.sendInfoTable.Remove("SCHM");
            }
            InfoUtils.sendInfoTable.Add("SCHM", str);
                try
                {
                    this.mobileGridView.Rows.RemoveAt(this.mobileGridView.SelectedRows[0].Index);
                }
                catch (Exception es)
                {
                    Console.WriteLine(es.Message);
                    MessageBox.Show("前选择数据后，再进行删除操作。"); 
                }
        }

        private void centerNum_Click(object sender, EventArgs e)
        {
            string passwd = string.Empty;
            if (InfoUtils.parTable.Contains("passwd"))
            {
                passwd = (string)InfoUtils.parTable["passwd"];
            }
            else
            {
                passwd = "000000";
            }
            string str = "ZXHM," + passwd + "," + this.textBox1.Text + ",";

            if (InfoUtils.sendInfoTable.ContainsKey("ZXHM"))
            {
                InfoUtils.sendInfoTable.Remove("ZXHM");
            }
                InfoUtils.sendInfoTable.Add("ZXHM", str);
        }

        private void mobileGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        private void mobileGridView_RowClick(object sender, EventArgs e)
        {
            if (this.mobileGridView.SelectedRows.Count>0)
            this.textBox1.Text = this.mobileGridView.SelectedRows[0].Cells[0].Value.ToString();
        }

        private void mobileRefresh_Click(object sender, EventArgs e)
        {
            List<string> list = (List<string>)InfoUtils.parTable["mobile"];
            for (int i = 0; i < list.Count; i++)
            {
                this.mobileGridView.Rows[i].Cells[0].Value = list[i];
            }
            this.mobileGridView.ClearSelection();
        }
        
    }
}
